package com.usian.admin.kafka;

import com.usian.admin.service.WemediaNewsAutoScanService;
import com.usian.common.kafka.NewsAutoScanConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class WemediaNewsAutoListener {

    @Autowired
    WemediaNewsAutoScanService wemediaNewsAutoScanService;

    /**
     * 监听器  每当有消息，就执行一次这个方法
     * **/
    @KafkaListener(topics = NewsAutoScanConstant.WM_NEWS_AUTO_SCAN_TOPIC)
    public void recevieMessage(ConsumerRecord<String,String> record){
        //就硬他妈转换  ofNullable(？)不判空的转换
        Optional<? extends ConsumerRecord<String, String>> optional = Optional.ofNullable(record);
        /* 判空  */
        if(optional.isPresent()){
            //key：topic   value：消息内容
            Object value = record.value();
            wemediaNewsAutoScanService.autoScanByMediaNewsId(Long.valueOf((String) value));
        }

    }
}